package com.wh.topic;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.wh.utils.ConnectionUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Auther: HuiEr
 * @Date: 2020/3/11 21:58
 * @Description
 */
public class Consumer2 {
    private static final String EXCHANGE_NAME="topic_exchange";
    private static final String EXCHANGE_TYPE="topic";
    private static final String ROUTER_KEY="hui.hui";
    private static final String QUEUE_NAME="topic_queue_2";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = ConnectionUtil.getConnection().createChannel();
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        channel.exchangeDeclare(EXCHANGE_NAME,EXCHANGE_TYPE);
        channel.queueBind(QUEUE_NAME,EXCHANGE_NAME,ROUTER_KEY);
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("消费者2号接收的消息    "+new String(body));
            }
        };
        channel.basicConsume(QUEUE_NAME,false,defaultConsumer);
    }
}
